﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using TTQT.TTQT_Code;

/// <summary>
/// Summary description for SanPham
/// </summary>
public class NewsHtmlDAO
{

    public string id = "";
    public string tenTrang = "";
    public string ghiChu = "";
    public string status = "";
    public string noiDung = "";
  
    //xong
    public int insert()
    {
        int kq = 0;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand cmd = new SqlCommand("NewsHtml_insert", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@tenTrang", tenTrang);
        cmd.Parameters.AddWithValue("@noiDung", noiDung);
        cmd.Parameters.AddWithValue("@ghiChu", ghiChu);
        cmd.Parameters.AddWithValue("@status", status);
        
        try
        {
            conn.Open();
            kq = TTQTCommon.ToInt32(cmd.ExecuteScalar(), 0);
            id = kq.ToString();
        }
        catch (Exception exxx)
        { }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
        return kq;
    }
    //XONG
    public bool getById()
    {
        bool kq = false;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("NewsHtml_getById", conn);
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@id", id);
        try
        {
            conn.Open();
            SqlDataReader dr = comm.ExecuteReader();
            if (dr.Read())
            {
                kq = true;
                id = TTQTCommon.ToString(dr["id"]);
                tenTrang = TTQTCommon.ToString(dr["tenTrang"]);
                noiDung = TTQTCommon.ToString(dr["noiDung"]);
                ghiChu = TTQTCommon.ToString(dr["ghiChu"]);
                status = TTQTCommon.ToString(dr["status"]);
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return kq;
    }
    public int update()
    {
        int kq = 0;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand cmd = new SqlCommand("NewsHtml_Update", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", id);
        cmd.Parameters.AddWithValue("@tenTrang", tenTrang);
        cmd.Parameters.AddWithValue("@noiDung", noiDung);
        cmd.Parameters.AddWithValue("@ghiChu", ghiChu);
        cmd.Parameters.AddWithValue("@status", status);
        try
        {
            conn.Open();
            kq = TTQTCommon.ToInt32(cmd.ExecuteNonQuery(), 0);
        }
        catch (Exception exxx)
        { }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
        return kq;
    }
    public int delete()
    {
        int kq = 0;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand cmd = new SqlCommand("NewsHtml_Delete", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", id);
        try
        {
            conn.Open();
            kq = TTQTCommon.ToInt32(cmd.ExecuteNonQuery(), 0);
        }
        catch (Exception exxx)
        { }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
        return kq;
    }
    //public DataTable timKiem(string keyWord, string nhomId, string loaiId, string giaTien)
    //{
    //    if (keyWord.Equals("Nhập từ khóa"))
    //    {
    //        keyWord = "";
    //    }
    //    DataTable result = null;
    //    string sql =
    //        "select SanPham.id,NhomId,LoaiId,Tenhang,SanPham_Nhom.[Name] as TenNhom,TomLuoc,NoiDung,GiaBanThamKhao,PicUrl,userName,convert(varchar(10),NgayNhap,103) as ngayNhap,hangmoi,active,del from SanPham left join SanPham_Nhom on sanpham.NhomId=SanPham_nhom.id";
    //    string whereClause = " where 1=1";
    //    if (!string.IsNullOrEmpty(keyWord))
    //    {
    //        whereClause += " and (tenHang like '%" + keyWord + "%' or tomLuoc like '%" + keyWord + "%' or noiDung like '%" + keyWord + "%')";
    //    }
    //    whereClause += " and (nhomId=" + nhomId + " or -1=" + nhomId + ")";
    //    whereClause += " and (loaiId=" + loaiId + " or -1=" + loaiId + ")";
    //    if (!giaTien.Equals("-1"))
    //    {
    //        whereClause += " and GiaBanThamKhao " + giaTien;
    //    }
    //    sql += whereClause;

    //    SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
    //    SqlCommand comm = new SqlCommand(sql, conn);

    //    comm.CommandType = CommandType.Text;
    //    try
    //    {
    //        conn.Open();
    //        result = new DataTable();
    //        SqlDataAdapter da = new SqlDataAdapter(comm);
    //        da.Fill(result);
    //    }
    //    catch (Exception exx)
    //    {
    //        // Writing log methods here.
    //    }
    //    finally
    //    {
    //        if (conn.State == ConnectionState.Open) conn.Close();
    //        conn.Dispose();
    //        comm.Dispose();
    //    }
    //    return result;

    //}


    public string NewsHtml_getNoiDungById()
    {
        string kq = null;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("NewsHtml_getNoiDungById", conn);
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Clear();
        comm.Parameters.Add("@id", SqlDbType.Int, 4).Value = id;
        try
        {
            conn.Open();
            kq = TTQTCommon.ToString(comm.ExecuteScalar().ToString());
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return kq;
    }
    public DataTable getAll()
    {
        DataTable result = null;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("NewsHtml_getAll", conn);
        comm.CommandType = CommandType.StoredProcedure;
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return result;
    }
}
